//
//  WelcomeViewController.swift
//  swift-GWYWeiBo
//
//  Created by x on 2017/4/11.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit
class WelcomeViewController: UIViewController {

    let WIDTH = UIScreen.main.bounds.size.width
    let HEIGHT = UIScreen.main.bounds.size.height
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //添加背景图片
        view.addSubview(bgImageView)
        //添加头像
        view.addSubview(headImageView)
        //添加标签lable
        view.addSubview(messageLable)
        
        //设置用户头像
        if (UserAccess.loadAccount()?.avatar_large) != nil {
            let url:URL = URL(string: (UserAccess.loadAccount()?.avatar_large)!)!
            headImageView.sd_setImage(with: url)
        }
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        //界面出现.开始执行头像动画
        UIView.animate(withDuration: 1.0, animations: {
            self.headImageView.center = CGPoint(x: self.WIDTH/2, y: 130)
        }) { (_) in
            print(NSStringFromCGRect(self.headImageView.frame))
            //开始执行文本动画
            UIView.animate(withDuration: 1.2, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 1.2, options: UIViewAnimationOptions.init(rawValue: 0), animations: { 
                self.messageLable.alpha = 1.0
            }, completion: { (_) in
                print("OK")
                //动画执行完毕要跳转
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: GWYSwitchRootController), object: true)
            })
        }
    }
    //MARK: -- 懒加载
    
    /// 加载背景图片
    private lazy var bgImageView = { () -> UIImageView in
        
        let imageView = UIImageView(image: UIImage.init(named: "ad_background"))
        imageView.frame = self.view.bounds
        return imageView
    }()
    
    /// 加载用户头像
    private lazy var headImageView = { () -> UIImageView in
        
        let imageView = UIImageView(image: UIImage.init(named: "avatar_default_big"))
        imageView.frame = CGRect(x: (self.WIDTH-100)/2, y: self.HEIGHT, width: 100, height: 100)
        imageView.layer.cornerRadius = 50
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    /// 懒加载消息标签
    private lazy var messageLable = { ()-> UILabel in
        
        let lable = UILabel()
        lable.frame = CGRect.init(x: 0, y: 180, width: self.WIDTH, height: 30)
        lable.textAlignment = NSTextAlignment.center
        lable.alpha = 0.0
        lable.text = "欢迎回来"
        return lable
    }()
}
